<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/weui/2.5.16/style/weui.min.css"
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />
  </head>
  <body>
    <div class="page uploader js_show" tabindex="-1">
      <div class="page__hd">
        <h1 class="page__title">Uploader</h1>
        <p id="x123" class="page__desc">
          <input
            class="weui-hidden_abs"
            readonly=""
            style="width: 1px; height: 1px"
            aria-labelledby="x123"
            role="option"
          />
          上传组件，一般配合<a class="weui-wa-hotarea link" href="#gallery"
            >组件Gallery</a
          >来使用。
        </p>
      </div>
      <div class="page__bd">
        <div
          role="dialog"
          aria-hidden="true"
          aria-modal="true"
          class="weui-gallery"
          id="gallery"
          wah-hotarea="click"
        >
          <span
            role="img"
            tabindex="0"
            class="weui-gallery__img"
            id="galleryImg"
          ></span>
          <div class="weui-gallery__opr">
            <a
              role="button"
              aria-label="删除"
              href="javascript:"
              class="weui-gallery__del"
            >
              <i class="weui-icon-delete weui-icon_gallery-delete"></i>
            </a>
          </div>
        </div>

        <div class="weui-cells weui-cells_form">
          <div class="weui-cell weui-cell_uploader">
            <div class="weui-cell__bd">
              <div class="weui-uploader">
                <div
                  class="weui-uploader__hd"
                  role="option"
                  aria-labelledby="js_uploader_title js_a11y_comma js_uploader_current_num js_uploader_unit js_a11y_comma js_uploader_max_tips js_uploader_max_num js_uploader_unit"
                >
                  <p id="js_uploader_title" class="weui-uploader__title">
                    图片上传
                  </p>
                  <div class="weui-uploader__info">
                    <span id="js_uploader_current_num">0</span>/<span
                      id="js_uploader_max_num"
                      >2</span
                    >
                  </div>
                  <div id="js_uploader_unit" class="weui-hidden_abs">张</div>
                  <div id="js_uploader_max_tips" class="weui-hidden_abs">
                    可上传
                  </div>
                </div>
                <div class="weui-uploader__bd">
                  <ul
                    class="weui-uploader__files"
                    id="uploaderFiles"
                    wah-hotarea="click"
                  >
                    <li
                      class="weui-uploader__file"
                      role="img"
                      aria-label="图片标题"
                      title="轻点两下查看大图"
                      tabindex="0"
                      style="
                        text-align: right;
                        background-image: url(./images/pic_160.png);
                      "
                    >
                      <i
                        class="weui-icon-delete weui-icon_gallery-delete"
                        style="color: red"
                      ></i>
                    </li>
                  </ul>
                  <div class="weui-uploader__input-box">
                    <input
                      id="uploaderInput"
                      class="weui-uploader__input"
                      type="file"
                      accept="image/*"
                      multiple=""
                    />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <a
          id="submit"
          href="javascript:"
          role="button"
          class="weui-btn weui-btn_primary"
          style="margin-top: 20px; margin-bottom: 20px"
          >提交</a
        >
      </div>
      <div class="page__ft j_bottom">
        <div class="weui-footer">
          <div class="weui-footer__text">
            <span class="weui-footer__text__meta"
              >公司地址：深圳市南山区粤海街道麻岭社区科技中一路腾讯大厦35层</span
            >
            <span class="weui-footer__text__meta">联系电话：4006 700 700</span>
          </div>
          <div class="weui-footer__text">
            Copyright © 1998 - <span id="js_copyright_year"></span> Tencent All
            Rights Reserved.
          </div>
        </div>
      </div>
    </div>
    <script
      crossorigin="anonymous"
      src="https://lib.baomitu.com/jquery/latest/jquery.min.js"
    ></script>
    <script>
      $(function () {
        let uploadFiles = [];

        $("#uploaderInput").on("change", function (event) {
          const files = event.target.files;
          const fileList = Array.from(files);
          const items = fileList.map(
            (file) => `<li
                      class="weui-uploader__file"
                      role="img"
                      aria-label="图片标题"
                      title="轻点两下查看大图"
                      tabindex="0"
                      style="
                        text-align: right;
                        background-image: url(${URL.createObjectURL(file)})"
                    >
                      <i
                        class="weui-icon-delete weui-icon_gallery-delete"
                        style="color: red"
                      ></i>
                    </li>`
          );
          $("#uploaderFiles").append(items);
          // 添加到待上传数组
          uploadFiles.push(fileList);
          // 下面这行代码是为了 当前后选择两张一样的照片时触发 change 事件
          event.target.value = "";
        });

        $("#uploaderFiles").on("click", (event) => {
          if (
            event.target.className ===
            "weui-icon-delete weui-icon_gallery-delete"
          ) {
            $(event.target).parent().remove();
            const index = $(event.target).index();
            // 从待上传数组里面删除
            uploadFiles.filter((_, i) => i !== index);
          }
        });

        // 上传
        $("#submit").on("click", () => {
          console.log(uploadFiles);
        });
      });
    </script>
  </body>
</html>
